//
//  NSDate+descrip.m
//  togetherLBS
//
//  Created by Phyllis Hu on 13-10-9.
//  Copyright (c) 2013年 celi. All rights reserved.
//

#import "NSDate+descrip.h"

@implementation NSDate (descrip)

- (NSString *)descriptionSinceNow
{
    double seconds = [self timeIntervalSinceNow];
    
    NSString *suffix = nil;
    if (seconds < 0) {
        suffix = CLLocalizedString(@"Ago", nil);
        seconds = -seconds;
    }else{
        suffix = CLLocalizedString(@"LaterSuffix", nil);
    }
    
    int minutes = seconds/60;
    if (minutes <= 2) {
        return CLLocalizedString(@"JustNow", nil);
    }
    
    int hours = minutes/60;
    if (hours < 1) {
        return [NSString stringWithFormat:@"%d%@%@",minutes,CLLocalizedString(@"Mins", nil),suffix];
    }
    int days = hours/24;
    if (days < 1) {
        return [NSString stringWithFormat:@"%d%@%@",hours,CLLocalizedString(@"Hours", nil),suffix];
    }
    
    int years = days/365;
    if (years < 1) {
        return [NSString stringWithFormat:@"%d%@%@",days,CLLocalizedString(@"Days", nil),suffix];
    }
    
    return [NSString stringWithFormat:@"%d%@%@",years,CLLocalizedString(@"Years", nil),suffix];
    
}

//包含表述“昨天” “前天”,可用于日志评论的时间显示
- (NSString *)descriptionSinceNowFocusOnDay
{
    //时间
    NSDateFormatter *theFormatter = [[NSDateFormatter alloc] init];
    [theFormatter setDateFormat:@"H:mm"];
    NSString *timeStr = [theFormatter stringFromDate:self];
    
    NSString *dayStr = [self dayDescriptionSinceNow];
    
    return [NSString stringWithFormat:@"%@ %@",dayStr,timeStr];
}
- (NSString *)dayDescriptionSinceNow
{
    NSString *dayStr = nil;
    NSDate *today = [NSDate date];
    NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow:-24*60*60];
    NSDate *beforeYesterday = [NSDate dateWithTimeIntervalSinceNow:-48*60*60];
    
    if ([self isTheSameDayWith:today]) {
        dayStr = CLLocalizedString(@"Today", nil);
    }else if([self isTheSameDayWith:yesterday]){
        dayStr = CLLocalizedString(@"Yesterday", nil);
    }else if ([self isTheSameDayWith:beforeYesterday]){
        NSString *strLang =[[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] firstObject];
        if ([strLang isEqualToString:@"zh-Hans"] || [strLang isEqualToString:@"zh-Hant"]) {
            dayStr = CLLocalizedString(@"BeforeYesterday", nil);
        }else{
            NSDateFormatter *theFormatter = [[NSDateFormatter alloc] init];
            NSString *formatStr = [NSDateFormatter dateFormatFromTemplate:@"MMMd" options:0 locale:[NSLocale currentLocale]];
            [theFormatter setDateFormat:formatStr];
            dayStr = [theFormatter stringFromDate:self];
        }
    }else{
        NSDateFormatter *theFormatter = [[NSDateFormatter alloc] init];
        NSString *formatStr = [NSDateFormatter dateFormatFromTemplate:@"MMMd" options:0 locale:[NSLocale currentLocale]];
        [theFormatter setDateFormat:formatStr];
        dayStr = [theFormatter stringFromDate:self];
    }
    return dayStr;
}


- (BOOL)isTheSameDayWith:(NSDate *)otherDate
{
    NSCalendar *theCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    [theCalendar setLocale:[NSLocale currentLocale]];
    NSDateComponents *otherDay = [theCalendar components:NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:otherDate];
    NSDateComponents *theDay = [theCalendar components:NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:self];
    
    return ([theDay day] == [otherDay day] &&
            [theDay month] == [otherDay month] &&
            [theDay year] == [otherDay year] &&
            [theDay era] == [otherDay era]);
}

//该日期 的 00:00:00
- (NSTimeInterval)dayBeginTime
{
    NSDateFormatter *dayFormatter = [[NSDateFormatter alloc] init];
    [dayFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dayStr = [dayFormatter stringFromDate:self];
    [dayFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *theDate = [dayFormatter dateFromString:[dayStr stringByAppendingString:@" 00:00:00"]];
    return [theDate timeIntervalSince1970];
}

/**
 *  比较两个时间的大小
 *
 *  @return dateOne>=dateTwo return YES ;dateOne < dateTwo return NO
 */
+ (BOOL)compareWithDateOne:(NSDate*)dateOne dateTwo:(NSDate*)dateTwo{

    NSTimeInterval dateOneValue = [dateOne timeIntervalSince1970];
    NSTimeInterval dateTwoValue = [dateTwo timeIntervalSince1970];
    
    if (dateOneValue >= dateTwoValue) return YES;

    return NO;
}

/**
 *  获得前一天或者后一天
 */
+ (NSDate *)setDayDate:(NSDate*)nowDate type:(setDayType)type{
    
    NSTimeInterval secondsPerDay = 24*60*60*type;
//    NSDate *day =[NSDate dateWithTimeInterval:secondsPerDay sinceDate:nowDate];
    NSDate *day = [nowDate dateByAddingTimeInterval:secondsPerDay];
    return day;
}


/**
 *  获取上一周或下一周
 */
+ (NSDate *)setWeekDate:(NSDate*)nowDate type:(setWeekType)type{

    NSTimeInterval secondsPerDay = 24*60*60*(7-1)*type;
    NSDate *day = [nowDate dateByAddingTimeInterval:secondsPerDay];
    return day;
}

/**
 *  把时间转化成字符串
 */
- (NSString*)dateConversionString{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //zzz表示时区，zzz可以删除，这样返回的日期字符将不包含时区信息。
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];
    NSString *destDateString = [dateFormatter stringFromDate:self];
    
    return destDateString;
}

/**
 *  把时间转化成字符串(去掉秒)
 */
- (NSString*)dateConversionString_clearS{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //zzz表示时区，zzz可以删除，这样返回的日期字符将不包含时区信息。
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *destDateString = [dateFormatter stringFromDate:self];
    
    return destDateString;
}

/**
 *  把时间转化成字符串(只有小时、分钟)
 */
- (NSString*)dateConversionString_Hm{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //zzz表示时区，zzz可以删除，这样返回的日期字符将不包含时区信息。
    [dateFormatter setDateFormat:@"HH:mm"];
    NSString *destDateString = [dateFormatter stringFromDate:self];
    
    return destDateString;
}


/**
 *  把时间转化成字符串(只有月份、日期)
 */
- (NSString*)dateConversionString_Md{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //zzz表示时区，zzz可以删除，这样返回的日期字符将不包含时区信息。
    [dateFormatter setDateFormat:@"MM-dd"];
    NSString *destDateString = [dateFormatter stringFromDate:self];
    
    return destDateString;
}

/**
 *  把时间转化成字符串(只有年、月份、日期)
 */
- (NSString*)dateConversionString_yMd{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //zzz表示时区，zzz可以删除，这样返回的日期字符将不包含时区信息。
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *destDateString = [dateFormatter stringFromDate:self];
    
    return destDateString;
}

/**
 *  把时间转化成字符串(只有月份、日期、时间)
 */
- (NSString*)dateConversionString_MdT{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //zzz表示时区，zzz可以删除，这样返回的日期字符将不包含时区信息。
    [dateFormatter setDateFormat:@"yyyy年MM月dd HH:mm"];
    NSString *destDateString = [dateFormatter stringFromDate:self];
    
    return destDateString;
}

/**
 *  字符串传时间
 */
+ (NSDate *)stringConverDate:(NSString *)dataString {
    
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_CN"] ];
    [inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate* date = [inputFormatter dateFromString:dataString];
    return date;
    
    
//    //UTC时间格式下的北京时间，可以看到北京时间＝ UTC + 8小时。
//    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mmZ"];
////    @"2013-08-03T04:56:52+0000"
//    NSDate *localDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@%@",dataString,@"+0000"]];//＋0000 表示的是当前时间是个世界时间。
//    return localDate;
    
}

@end
